﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Smartkernel
{
    /// <summary>
    /// 智能随机数类，提供多种产生随机数的方式，不光可以产生随机数字，还可以产生随机字符串等任何格式的字符
    /// </summary>
    public static class SmartRandom
    {
        private static Random random = new Random(DateTime.Now.Ticks.GetHashCode());
        private static RNGCryptoServiceProvider rngCryptoServiceProvider = new RNGCryptoServiceProvider();
        /// <summary>
        /// 产生指定范围的随机数，随机数可以取开始和结束两个边界值
        /// </summary>
        /// <param name="start">开始边界</param>
        /// <param name="end">结束边界</param>
        /// <returns>随机数</returns>
        public static int Next(int start, int end)
        {
            return random.Next(start, end + 1);
        }
        /// <summary>
        /// 产生指定范围和指定长度的随机字符串，随机字符串的长度不受范围长度的限制
        /// </summary>
        /// <param name="scope">随机字符串的范围，可以取任何字符串</param>
        /// <param name="length">需要返回的随机字符串的长度</param>
        /// <returns>随机字符串</returns>
        public static string Next(string scope, int length)
        {
            char[] chars = new char[scope.Length];
            chars = scope.ToCharArray();
            byte[] datas = new byte[1];
            rngCryptoServiceProvider.GetNonZeroBytes(datas);
            datas = new byte[length];
            rngCryptoServiceProvider.GetNonZeroBytes(datas);
            StringBuilder result = new StringBuilder(length);
            foreach (byte data in datas)
            {
                result.Append(chars[data % chars.Length]);
            }
            return result.ToString();
        }
        /// <summary>
        /// 获得随机大写字符串，字符串范围ABCDEFGHIJKLMNOPQRSTUVWXYZ
        /// </summary>
        /// <param name="length">字符串长度</param>
        /// <returns>随机字符串</returns>
        public static string NextCapitalString(int length)
        {
            string scope = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            return Next(scope, length);
        }
        /// <summary>
        /// 生成一个随机数学表达式
        /// </summary>
        /// <param name="result">表达式的计算结果</param>
        /// <returns>生成的随机表达式</returns>
        public static string NextExpression(out int result)
        {
            string expression = Next(1, 15).ToString() + " " + Next("+-*", 1) + " " + Next(1, 15).ToString();
            result = (int)SmartMath.Eval(expression);
            return expression;
        }
        /// <summary>
        /// 从列表中获得随机对象
        /// </summary>
        /// <typeparam name="T">列表中对象的类型</typeparam>
        /// <param name="list">对象列表</param>
        /// <returns>随机对象</returns>
        public static T Next<T>(List<T> list)
        {
            T[] ts = list.ToArray();
            return ts[Next(0, ts.Length - 1)];
        }
    }
}
